﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Common;

public partial class Stock_Invoice_Add : System.Web.UI.Page
{
    protected int InvoiceKey;

    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["menutype"]) ? "" : Request["menutype"].ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        InvoiceKey = string.IsNullOrEmpty(Request["InvoiceKey"]) ? 0 : Convert.ToInt32(Request["InvoiceKey"]);

        if (!this.IsPostBack)
        {
            IList<Sys_Account> UserList = new Account().GetNotAccount(1);
            foreach (Sys_Account account in UserList)
                txt_MUserKey.Items.Add(new ListItem("◆ " + account.RealName, account.UserKey.ToString()));

            txt_InvoiceDate.Attributes.Add("onclick","selectDate(this);");
            txt_ArriveDate.Attributes.Add("onclick", "selectDate(this);");

            if (InvoiceKey != 0)
            {
                Dat_Invoice invoice = new Invoice().SelectInvoice(InvoiceKey);

                txt_DanNum.Text = invoice.DanNum;
                txt_CustName.Text = invoice.CustName;
                txt_InvoiceDate.Text = invoice.InvoiceDate.ToString("yyyy-MM-dd");
                txt_InvoiceNO.Text = invoice.InvoiceNO;
                txt_InvoiceContent.Text = invoice.InvoiceContent;
                txt_InvoicePrice.Text = invoice.InvoicePrice.ToString();
                txt_ArriveDate.Text = invoice.ArriveDate.ToString().Split(' ')[0];
                txt_MUserKey.SelectedValue = invoice.MUserKey.ToString();

                btnSubmit.Text = "修 改";
            }
       }
    }

    //添加
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime? de = null;

        if (InvoiceKey == 0)
        {
            Dat_Invoice invoice = new Dat_Invoice
            {
                DanNum = txt_DanNum.Text.Trim(),
                CustName = txt_CustName.Text.Trim(),
                InvoiceDate = Convert.ToDateTime(txt_InvoiceDate.Text.Trim()),
                InvoiceNO = txt_InvoiceNO.Text.Trim(),
                InvoiceContent = txt_InvoiceContent.Text.Trim(),
                InvoicePrice = Convert.ToDecimal(txt_InvoicePrice.Text.Trim()),
                ArriveDate = txt_ArriveDate.Text.Trim() == "" ? de : Convert.ToDateTime(txt_ArriveDate.Text.Trim()),
                MUserKey = Convert.ToInt32(txt_MUserKey.SelectedValue),
                CreateTime = DateTime.Now,
                UserKey = Convert.ToInt32(Session["UserKey"])
            };

            if (new Invoice().AddInvoice(invoice))
            {
                WebUtility.JsAlertToGo("添加发票成功", "Invoice_Add.aspx?menutype=331");
            }
        }
        else
        {
            if (Convert.ToInt32(Session["UserType"]) == 4)     //管理员修改
            {
                Dat_Invoice invoice = new Dat_Invoice
                {
                    InvoiceKey = InvoiceKey,
                    DanNum = txt_DanNum.Text.Trim(),
                    CustName = txt_CustName.Text.Trim(),
                    InvoiceDate = Convert.ToDateTime(txt_InvoiceDate.Text.Trim()),
                    InvoiceNO = txt_InvoiceNO.Text.Trim(),
                    InvoiceContent = txt_InvoiceContent.Text.Trim(),
                    InvoicePrice = Convert.ToDecimal(txt_InvoicePrice.Text.Trim()),
                    ArriveDate = txt_ArriveDate.Text.Trim() == "" ? de : Convert.ToDateTime(txt_ArriveDate.Text.Trim()),
                    MUserKey = Convert.ToInt32(txt_MUserKey.SelectedValue)

                };

                if (new Invoice().EditInvoice(invoice))
                {
                    WebUtility.JsAlertToGo("修改发票成功", "Invoice.aspx?menutype=332");
                }
            }

            if (Convert.ToInt32(Session["UserType"]) == 3) //工程师修改
            {
                Dat_Invoice invoice = new Dat_Invoice
                {
                    InvoiceKey = InvoiceKey,
                    DanNum = txt_DanNum.Text.Trim()
                };

                if (new Invoice().EditInvoiceNO(invoice))
                {
                    WebUtility.JsAlertToGo("修改发票预约序号成功", "Invoice.aspx?menutype=332");
                }
            }
        }
    }

}
